﻿#include "ggbondbtnprogressbar.h"
#include <QPainter>

GGbondBtnProgressBar::GGbondBtnProgressBar(QWidget *parent) :
    QWidget(parent)
{
    lineWidth = 20;
    lineColor = QColor("#ffffff");
    borderWidth = 2;
    borderColor = QColor("#22a3a9");
    borderRadius = 2;
    bgColor = QColor("#22a3a9");
    value = 0;
    status = Status_Start;
    tempWidth = this->width();

    startText = u8"开始";
    stopText = u8"完成";
    valueMax = 360.0;

    timer = new QTimer();
//    QObject::connect(timer, &QTimer::timeout, [&]() {
//        value += 1;
//    });
    connect(timer,SIGNAL(timeout()),this,SLOT(progress()));
}

GGbondBtnProgressBar::~GGbondBtnProgressBar()
{

}

void GGbondBtnProgressBar::resizeEvent(QResizeEvent *)
{
    tempWidth = this->width();
}

void GGbondBtnProgressBar::mousePressEvent(QMouseEvent *)
{
    //处理鼠标按下事件
    if(Status_Ing == status){
        return; //绘制进度过程中不应该使其响应
    }
    else if(Status_Start == status){
        status = 1;
        timer->start(10);
    }
    else if(Status_Stop == status){
        status = 0;
        update();
    }
}

void GGbondBtnProgressBar::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    if (1 == status) {
        //绘制当前进度
        drawProgress(&painter);
    } else {
        //绘制按钮背景
        drawBg(&painter);
    }
}

void GGbondBtnProgressBar::drawBg(QPainter *painter)
{
    painter->save();

    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    QPen pen;
    pen.setWidth(borderWidth);
    pen.setColor(borderColor);
    painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
    painter->setBrush(bgColor);

    QRect rect(((width - tempWidth) / 2) + borderWidth, borderWidth, tempWidth - (borderWidth * 2), height - (borderWidth * 2));
    painter->drawRoundedRect(rect, borderRadius, borderRadius);

    QFont font;
    font.setPixelSize(side - 18);
    painter->setFont(font);
    painter->setPen(lineColor);
    painter->drawText(rect, Qt::AlignCenter, status == 2 ? stopText : startText);

    painter->restore();
}

void GGbondBtnProgressBar::drawProgress(QPainter *painter)
{
    painter->save();

    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);
    int radius = 99 - borderWidth;

    //绘制外圆
    QPen pen;
    pen.setWidth(borderWidth);
    pen.setColor(borderColor);
    painter->setPen(borderWidth > 0 ? pen : Qt::NoPen);
    painter->setBrush(bgColor);

    //平移坐标轴中心,等比例缩放
    QRect rectCircle(-radius, -radius, radius * 2, radius * 2);
    painter->translate(width / 2, height / 2);
    painter->scale(side / 200.0, side / 200.0);
    painter->drawEllipse(rectCircle);

    //绘制圆弧进度
    pen.setWidth(lineWidth);
    pen.setColor(lineColor);
    painter->setPen(pen);

    int offset = radius - lineWidth - 5;
    QRectF rectArc(-offset, -offset, offset * 2, offset * 2);
    int startAngle = offset * 16;
    int spanAngle = -value * 16;
    painter->drawArc(rectArc, startAngle, spanAngle);

    //绘制进度文字
    QFont font;
    font.setPixelSize(offset - 15);
    painter->setFont(font);
    QString strValue = QString("%1%").arg((int)value  * 100 / 360);
    painter->drawText(rectCircle, Qt::AlignCenter, strValue);

    painter->restore();
}

int GGbondBtnProgressBar::getLineWidth() const
{
    return lineWidth;
}

QColor GGbondBtnProgressBar::getLineColor() const
{
    return lineColor;
}

int GGbondBtnProgressBar::getBorderWidth() const
{
    return borderWidth;
}

QColor GGbondBtnProgressBar::getBorderColor() const
{
    return borderColor;
}

int GGbondBtnProgressBar::getBorderRadius() const
{
    return borderRadius;
}

QColor GGbondBtnProgressBar::getBgColor() const
{
    return bgColor;
}

QString GGbondBtnProgressBar::getStartText() const
{
    return startText;
}

QString GGbondBtnProgressBar::getStopText() const
{
    return stopText;
}

double GGbondBtnProgressBar::getValueMax() const
{
    return valueMax;
}

QSize GGbondBtnProgressBar::sizeHint() const
{
    return QSize(200,60);
}

QSize GGbondBtnProgressBar::minimumSizeHint() const
{
    return QSize(60,30);
}

void GGbondBtnProgressBar::progress()
{
    if (0 == status) {
        tempWidth -= 5;
        if (tempWidth < this->height() / 2) {
            tempWidth = this->height() / 2;
            status = 1;
        }
    } else if (1 == status) {
        value += /*1.0*/360.0/valueMax;
        emit valueChanged(value); //添加信号发射，方便通信使用

        if (value >= 360) {
            status = 2;
            value = 0; //完成之后value置为0
        }
    } else if (2 == status) {
        tempWidth += 5;
        if (tempWidth > this->width()) {
            tempWidth = this->width();
            timer->stop();
        }
    }

    this->update();
}

void GGbondBtnProgressBar::setLineWidth(int lineWidth)
{
    if(this->lineWidth != lineWidth){
        this->lineWidth = lineWidth;
        this->update();
    }
}

void GGbondBtnProgressBar::setLineColor(const QColor &lineColor)
{
    if(this->lineColor != lineColor){
        this->lineColor = lineColor;
        this->update();
    }
}

void GGbondBtnProgressBar::setBorderWidth(int borderWidth)
{
    if(this->borderWidth != borderWidth){
        this->borderWidth = borderWidth;
        this->update();
    }
}

void GGbondBtnProgressBar::setBorderColor(const QColor &borderColor)
{
    if(this->borderColor != borderColor){
        this->borderColor = borderColor;
        this->update();
    }
}

void GGbondBtnProgressBar::setBorderRadius(int borderRadius)
{
    if(this->borderRadius != borderRadius){
        this->borderRadius = borderRadius;
        this->update();
    }
}

void GGbondBtnProgressBar::setBgColor(const QColor &bgColor)
{
    if(this->bgColor != bgColor){
        this->bgColor = bgColor;
        this->update();
    }
}

void GGbondBtnProgressBar::setStartText(const QString &startText)
{
    if(this->startText != startText){
        this->startText = startText;
        this->update();
    }
}

void GGbondBtnProgressBar::setStopText(const QString &stopText)
{
    if(this->stopText != stopText){
        this->stopText = stopText;
        this->update();
    }
}

void GGbondBtnProgressBar::setValueMax(const double &valueMax)
{
    if(this->valueMax != valueMax){
        this->valueMax = valueMax;
        this->update();
    }
}
